package com.application.tourapp.baseadapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.tourapp.R;
import com.application.tourapp.model.Type;

public class TypeListBaseAdapter extends BaseAdapter {
	private static ArrayList<Type> ItemrrayList;
	
	private Integer[] imgid = {
			R.drawable.unkown
			};
	
	private LayoutInflater l_Inflater;

	public TypeListBaseAdapter(Context context, ArrayList<Type> results) {
		ItemrrayList = results;
		l_Inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return ItemrrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return ItemrrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/*
    	 * EGA - la modification des attribue de la classe des types
    	 */
		ViewHolder holder;
		if (convertView == null) {
			convertView = l_Inflater.inflate(R.layout.type_details_view, null);
			holder = new ViewHolder();
			holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
			holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		/*
    	 * EGA - la modification des attribue de la classe des types
    	 */
		
		holder.txt_itemName.setText(ItemrrayList.get(position).getName());
		holder.txt_itemName.setTextColor(Color.parseColor("#FF0000"));
		/*String blobs=ItemrrayList.get(position).getImageBlob();
		InputStream is;
		//byte[] blob = blobs.getBytes();
		
		//byte[] blob = Base64.encode(blobs.getBytes(),0);
		UUID uuid = UUID.fromString(blobs);
		ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
		bb.putLong(uuid.getMostSignificantBits());
		bb.putLong(uuid.getLeastSignificantBits());
		byte[] blob = bb.array();*/
		
		
		//byte[] decodedString = Base64.decode(blob, Base64.DEFAULT);
		byte[] blob = ItemrrayList.get(position).getImageBlob();
		Bitmap bmp = BitmapFactory.decodeByteArray(blob,0,blob.length);
		holder.itemImage.setImageBitmap(bmp);
		//holder.itemImage.setImageResource(imgid[ItemrrayList.get(position).getImageNumber() - 1]);
//		imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);

		return convertView;
	}

	static class ViewHolder {
		TextView txt_itemName;
		TextView txt_itemDescription;
		TextView txt_itemPrice;
		ImageView itemImage;
	}
}
